import mongoose, { Document, Schema } from 'mongoose';

export interface IFile extends Document {
  filename: string;
  originalname: string;
  path: string;
  size: number;
  mimetype: string;
  uploader: mongoose.Types.ObjectId;
  relatedLessonPlan?: mongoose.Types.ObjectId;
  downloads: number;
  isPublic: boolean;
  createdAt: Date;
  updatedAt: Date;
}

const fileSchema = new Schema<IFile>({
  filename: {
    type: String,
    required: true,
  },
  originalname: {
    type: String,
    required: true,
  },
  path: {
    type: String,
    required: true,
  },
  size: {
    type: Number,
    required: true,
  },
  mimetype: {
    type: String,
    required: true,
  },
  uploader: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
  },
  relatedLessonPlan: {
    type: Schema.Types.ObjectId,
    ref: 'LessonPlan',
  },
  downloads: {
    type: Number,
    default: 0,
  },
  isPublic: {
    type: Boolean,
    default: true,
  },
}, {
  timestamps: true,
});

// 创建索引
fileSchema.index({ uploader: 1 });
fileSchema.index({ relatedLessonPlan: 1 });
fileSchema.index({ createdAt: -1 });

export const File = mongoose.model<IFile>('File', fileSchema);